<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Building;
use App\Api\Helpers\ApiResponse;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BuildingController extends AdminController
{
    use ApiResponse;
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Building(), function (Grid $grid) {
            $grid->id->sortable();
            $grid->house_id;
            $grid->name;
            $grid->floor;
            $grid->description;
            $grid->image;
            $grid->is_rent;
            $grid->is_elevator;
            $grid->public_utilities;
            $grid->created_at;
            $grid->updated_at->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Building(), function (Show $show) {
            $show->id;
            $show->house_id;
            $show->name;
            $show->floor;
            $show->description;
            $show->image;
            $show->is_rent;
            $show->is_elevator;
            $show->public_utilities;
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Building(), function (Form $form) {
            $form->display('id');
            $form->text('house_id');
            $form->text('name');
            $form->text('floor');
            $form->text('description');
            $form->text('image');
            $form->text('is_elevator');
            $form->text('public_utilities');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function buildingApi(Request $request)
    {
        $houseId = $request->get('q');
        return \App\Models\Building::query()->where('is_rent', false)->where('house_id', $houseId)->get(['id', DB::raw('name as text')]);
    }

    public function getByHouseId($houseId, Request $request)
    {
        $buildings = \App\Models\Building::query()->where('house_id', $houseId)->get();
        return $this->success($buildings);
    }
}
